<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>jsondatasource.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_datasource_jsondatasource.js.html">jsondatasource.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Implementation of DataNode for wrapping JSON data.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.ds.JsonDataSource&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.Uri&#39;);
<a name="line24"></a>goog.require(&#39;goog.dom&#39;);
<a name="line25"></a>goog.require(&#39;goog.ds.DataManager&#39;);
<a name="line26"></a>goog.require(&#39;goog.ds.JsDataSource&#39;);
<a name="line27"></a>goog.require(&#39;goog.ds.LoadState&#39;);
<a name="line28"></a>goog.require(&#39;goog.ds.logger&#39;);
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>/**
<a name="line33"></a> * Data source whose backing is a JSON-like service, in which
<a name="line34"></a> * retreiving the resource specified by URL with the additional parameter
<a name="line35"></a> * callback. The resource retreived is executable JavaScript that
<a name="line36"></a> * makes a call to the named function with a JavaScript object literal
<a name="line37"></a> * as the only parameter.
<a name="line38"></a> *
<a name="line39"></a> * Example URI could be:
<a name="line40"></a> * http://www.google.com/data/search?q=monkey&amp;callback=mycb
<a name="line41"></a> * which might return the JS:
<a name="line42"></a> * mycb({searchresults:
<a name="line43"></a> *   [{uri: &#39;http://www.monkey.com&#39;, title: &#39;Site About Monkeys&#39;}]});
<a name="line44"></a> *
<a name="line45"></a> * TODO(user): Evaluate using goog.net.Jsonp here.
<a name="line46"></a> *
<a name="line47"></a> * A URI of an empty string will mean that no request is made
<a name="line48"></a> * and the data source will be a data source with no child nodes
<a name="line49"></a> *
<a name="line50"></a> * @param {string|goog.Uri} uri URI for the request.
<a name="line51"></a> * @param {string} name Name of the datasource.
<a name="line52"></a> * @param {string=} opt_callbackParamName The parameter name that is used to
<a name="line53"></a> *     specify the callback. Defaults to &#39;callback&#39;.
<a name="line54"></a> *
<a name="line55"></a> * @extends {goog.ds.JsDataSource}
<a name="line56"></a> * @constructor
<a name="line57"></a> * @final
<a name="line58"></a> */
<a name="line59"></a>goog.ds.JsonDataSource = function(uri, name, opt_callbackParamName) {
<a name="line60"></a>  goog.ds.JsDataSource.call(this, null, name, null);
<a name="line61"></a>  if (uri) {
<a name="line62"></a>    this.uri_ = new goog.Uri(uri);
<a name="line63"></a>  } else {
<a name="line64"></a>    this.uri_ = null;
<a name="line65"></a>  }
<a name="line66"></a>
<a name="line67"></a>  /**
<a name="line68"></a>   * This is the callback parameter name that is added to the uri.
<a name="line69"></a>   * @type {string}
<a name="line70"></a>   * @private
<a name="line71"></a>   */
<a name="line72"></a>  this.callbackParamName_ = opt_callbackParamName || &#39;callback&#39;;
<a name="line73"></a>
<a name="line74"></a>};
<a name="line75"></a>goog.inherits(goog.ds.JsonDataSource, goog.ds.JsDataSource);
<a name="line76"></a>
<a name="line77"></a>
<a name="line78"></a>/**
<a name="line79"></a> * Default load state is NOT_LOADED
<a name="line80"></a> * @private
<a name="line81"></a> */
<a name="line82"></a>goog.ds.JsonDataSource.prototype.loadState_ = goog.ds.LoadState.NOT_LOADED;
<a name="line83"></a>
<a name="line84"></a>
<a name="line85"></a>/**
<a name="line86"></a> * Map of all data sources, needed for callbacks
<a name="line87"></a> * Doesn&#39;t work unless dataSources is exported (not renamed)
<a name="line88"></a> */
<a name="line89"></a>goog.ds.JsonDataSource[&#39;dataSources&#39;] = {};
<a name="line90"></a>
<a name="line91"></a>
<a name="line92"></a>/**
<a name="line93"></a> * Load or reload the backing data for this node.
<a name="line94"></a> * Fires the JsonDataSource
<a name="line95"></a> * @override
<a name="line96"></a> */
<a name="line97"></a>goog.ds.JsonDataSource.prototype.load = function() {
<a name="line98"></a>  if (this.uri_) {
<a name="line99"></a>    // NOTE: &quot;dataSources&quot; is expose above by name so that it will not be
<a name="line100"></a>    // renamed.  It should therefore be accessed via array notation here so
<a name="line101"></a>    // that it also doesn&#39;t get renamed and stops the compiler from complaining
<a name="line102"></a>    goog.ds.JsonDataSource[&#39;dataSources&#39;][this.dataName_] = this;
<a name="line103"></a>    goog.log.info(goog.ds.logger, &#39;Sending JS request for DataSource &#39; +
<a name="line104"></a>        this.getDataName() + &#39; to &#39; + this.uri_);
<a name="line105"></a>
<a name="line106"></a>    this.loadState_ = goog.ds.LoadState.LOADING;
<a name="line107"></a>
<a name="line108"></a>    var uriToCall = new goog.Uri(this.uri_);
<a name="line109"></a>    uriToCall.setParameterValue(this.callbackParamName_,
<a name="line110"></a>        &#39;JsonReceive.&#39; + this.dataName_);
<a name="line111"></a>
<a name="line112"></a>    goog.global[&#39;JsonReceive&#39;][this.dataName_] =
<a name="line113"></a>        goog.bind(this.receiveData, this);
<a name="line114"></a>
<a name="line115"></a>    var scriptEl = goog.dom.createDom(&#39;script&#39;, {&#39;src&#39;: uriToCall});
<a name="line116"></a>    goog.dom.getElementsByTagNameAndClass(&#39;head&#39;)[0].appendChild(scriptEl);
<a name="line117"></a>  } else {
<a name="line118"></a>    this.root_ = {};
<a name="line119"></a>    this.loadState_ = goog.ds.LoadState.NOT_LOADED;
<a name="line120"></a>  }
<a name="line121"></a>};
<a name="line122"></a>
<a name="line123"></a>
<a name="line124"></a>/**
<a name="line125"></a> * Gets the state of the backing data for this node
<a name="line126"></a> * @return {goog.ds.LoadState} The state.
<a name="line127"></a> * @override
<a name="line128"></a> */
<a name="line129"></a>goog.ds.JsonDataSource.prototype.getLoadState = function() {
<a name="line130"></a>  return this.loadState_;
<a name="line131"></a>};
<a name="line132"></a>
<a name="line133"></a>
<a name="line134"></a>/**
<a name="line135"></a> * Receives data from a Json request
<a name="line136"></a> * @param {Object} obj The JSON data.
<a name="line137"></a> */
<a name="line138"></a>goog.ds.JsonDataSource.prototype.receiveData = function(obj) {
<a name="line139"></a>  this.setRoot(obj);
<a name="line140"></a>  this.loadState_ = goog.ds.LoadState.LOADED;
<a name="line141"></a>  goog.ds.DataManager.getInstance().fireDataChange(this.getDataName());
<a name="line142"></a>};
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>/**
<a name="line146"></a>* Temp variable to hold callbacks
<a name="line147"></a>* until BUILD supports multiple externs.js files
<a name="line148"></a>*/
<a name="line149"></a>goog.global[&#39;JsonReceive&#39;] = {};
</pre>


</body>
</html>
